package com.broadengate.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.broadengate.form.AdminLoginForm;


import com.broadengate.service.AdminService;


public class AdminLoginAction extends DispatchAction{
	private AdminService adminService;

	/******
	 * 管理员登陆，
	 * 核心功能由调用AdminService对象的方法login()实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		try {		
			HttpSession session=request.getSession();
			AdminLoginForm loginForm = (AdminLoginForm) form;
			System.out.println("AdminLoginAction  进入");
			boolean flag = adminService.login(loginForm.getAdminName(),loginForm.getPassword());
			if(flag){
				session.setAttribute("adminName",loginForm.getAdminName()); 
				return mapping.findForward("success");
			}
			else{
				session.setAttribute("adminName","0"); 
				return mapping.findForward("fail");
			}
		} catch (Exception e) {
			HttpSession session=request.getSession();
			session.setAttribute("adminName","0");
			e.printStackTrace();
			return mapping.findForward("fail");
		}
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
}
